using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TCEntity;

namespace TCDbService
{
  public class ShareService
  {
    readonly TCDbContext _context;
    readonly AccountService _account;
    public ShareService(TCDbContext context, AccountService accountService)
    {
      _context = context;
      _account = accountService;
    }
    async Task AuthAsync(User user, int communityId)
    {
      await _account.LoginAsync(user);
      if (!_context.Users_Communities.Any(entity => entity.CommunityId == communityId && entity.Username == user.Username))
        throw new ArgumentException("未加入该社区。");
    }
    public async Task ShareAsync(User user, int imageId, int communityId)
    {
      await AuthAsync(user, communityId);
      var image = await _context.Images.FindAsync(imageId);
      if (user.Username != image.Username) throw new ArgumentException("该照片不属于你。");
      _context.Add(new SharedImage()
      {
        ImageId = imageId,
        CommunityId = communityId
      });
      await _context.SaveChangesAsync();
    }
    public async Task ShareAsync(Image image)
    {
      var item = await _context.Images.FindAsync(image.Id);
      if (item == null)
      {
        _context.Images.Add(image);
      }
      else item.ImageCategory = image.ImageCategory;
      await _context.SaveChangesAsync();
    }

    public async Task<List<(Image, int)>> ViewSharedAsync(User user, int communityId)
    {
      await AuthAsync(user, communityId);
      var shares = await _context.SharedImages
        .Where(entity => entity.CommunityId == communityId)
        .ToListAsync();
      if (!shares.Any()) throw new ArgumentException("该社区没有已分享的照片。");
      var result = new List<(Image, int)>(shares.Count);
      foreach (var item in shares)
      {
        result.Add((await _context.Images.FindAsync(item.ImageId), item.Id));
      }
      return result;
    }
    public async Task CancelShareAsync(User user, int shareId)
    {
      var share = await _context.SharedImages.FindAsync(shareId);
      if (share == null) throw new ArgumentException("未找到指定分享。");
      await AuthAsync(user, share.CommunityId);
      var image = await _context.Images.FindAsync(share.ImageId);
      if (image.Username != user.Username) throw new ArgumentException("该分享不属于你。");
      _context.Remove(share);
      await _context.SaveChangesAsync();
    }
    public async Task QuitAsync(User user, int communityId)
    {
      await AuthAsync(user, communityId);
      var shares = await _context.SharedImages.Where(entity => entity.CommunityId == communityId).ToArrayAsync();
      foreach (var item in shares)
      {
        var image = await _context.Images.FindAsync(item.ImageId);
        if (image.Username == user.Username)
        {
          _context.Remove(item);
        }
      }
      await _context.SaveChangesAsync();
    }
    public async Task DeleteAsync(User user)
    {
      await _account.LoginAsync(user);
      var images = await _context.Images.Where(image => image.Username == user.Username).ToArrayAsync();
      foreach (var item in images)
      {
        var shares = await _context.SharedImages.Where(share => share.ImageId == item.Id).ToArrayAsync();
        _context.RemoveRange(shares);
      }
      await _context.SaveChangesAsync();
    }
  }
}
